
//
//  ZYRegularCheckInController.m
//  优学荟管理系统
//
//  Created by edz on 2018/1/5.
//  Copyright © 2018年 星道三好. All rights reserved.
//
#import "ProgramStudyViewController.h"
#import "ZYRegularCheckInController.h"
#import "ZYAllSegment.h"
#import "ZYPeopleOneController.h"
#import "ZYStrengthenClassController.h"
#import "ZYGoldMedalsController.h"
//Segment高度
#define kSegmentH 40
#define kScreenWidth [UIScreen mainScreen].bounds.size.width
#define kScreenHeight [UIScreen mainScreen].bounds.size.height
@interface ZYRegularCheckInController ()<UIScrollViewDelegate,ZYAllSegmentDelegate,UITextFieldDelegate>
@property (nonatomic, strong) UIScrollView *contentScrollView;
@property (nonatomic, strong) ZYAllSegment *segment;
@property (nonatomic,strong) UIView *allView;
@property (nonatomic,strong) UITextField *searchTextField;
@property (nonatomic,strong) ZYGoldMedalsController  *video;
@property (nonatomic,strong) ZYStrengthenClassController *live;
@property (nonatomic,strong)   ZYPeopleOneController *nearby;
@property (nonatomic,assign) NSInteger page;
@property (nonatomic,strong) UIButton *searchButton;
@end

@implementation ZYRegularCheckInController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self setSearchName];
    
    
    //加载Segment
    [self setSegment];
    //加载ViewController
    [self addChildViewController];
    //加载ScrollView
    [self setContentScrollView];
    
}


- (void)setSearchName{
    
    self.allView = [[UIView alloc]initWithFrame:CGRectMake(40, 0, kScreenWidth - 80, 30)];
    self.allView.backgroundColor = ColorRGB(247,245,245);
    self.allView.layer.cornerRadius = 15;
    self.allView.clipsToBounds = YES;
    self.navigationItem.titleView = self.allView;
    
    self.searchTextField = [[UITextField alloc]initWithFrame:CGRectMake(0, 0, self.allView.frame.size.width, self.allView.frame.size.height)];
    self.searchTextField.font = [UIFont systemFontOfSize:13];
    self.searchTextField.textColor = ColorRGB(102, 102, 102);
    self.searchTextField.textAlignment = NSTextAlignmentCenter;
    self.searchTextField.returnKeyType =  UIReturnKeySearch;
    self.searchTextField.clearButtonMode = UITextFieldViewModeAlways;
    self.searchTextField.delegate = self;
    [self.allView addSubview:self.searchTextField];
    
    
    self.searchButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [self.searchButton setImage:[UIImage imageNamed:@"icon_serch_default"] forState:(UIControlStateNormal)];
    [self.searchButton setTitle:@"  搜索课程或老师" forState:(UIControlStateNormal)];
    [self.searchButton setTitleColor:ColorRGB(102, 102, 102) forState:(UIControlStateNormal)];
    self.searchButton.titleLabel.font = [UIFont systemFontOfSize:13];
    self.searchButton.frame = self.searchTextField.frame;
    [self.allView addSubview:self.searchButton];
    
    [self.searchButton addTarget:self action:@selector(searchButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
    [self.searchTextField addTarget:self action:@selector(searchTextFieldAction:) forControlEvents:(UIControlEventEditingChanged)];
    
    
}


- (void)searchButtonAction:(UIButton *)sender{
    
    
    [self.searchTextField becomeFirstResponder];
    
}

- (void)searchTextFieldAction:(UITextField *)textField{
    
    
    self.searchButton.hidden = textField.text.length;
    
    
    
    
}











-(void)setSegment {
    
    /** qi签到*/
    //初始化
    ZYAllSegment *segment = [[ZYAllSegment alloc]initWithFrame:CGRectMake(0,0, self.view.frame.size.width, kSegmentH)];
    segment.delegate = self;
    self.segment = segment;
    self.segment.titleList = [[NSMutableArray alloc]initWithObjects:@"金牌课程",@"加强课",@"三好小当家", @"少年编程", nil];
    [self.view addSubview:segment];
    
    
}
//加载ScrollView
-(void)setContentScrollView {
    
    UIScrollView *sv = [[UIScrollView alloc]initWithFrame:CGRectMake(0,kSegmentH, self.view.frame.size.width, self.view.frame.size.height - kSegmentH)];
    [self.view addSubview:sv];
    sv.bounces = NO;
    sv.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
    sv.contentOffset = CGPointMake(0, 0);
    sv.pagingEnabled = YES;
    sv.showsHorizontalScrollIndicator = NO;
    sv.scrollEnabled = YES;
    sv.userInteractionEnabled = YES;
    sv.delegate = self;
    
    for (int i=0; i<self.childViewControllers.count; i++) {
        UIViewController * vc = self.childViewControllers[i];
        vc.view.frame = CGRectMake(i * kScreenWidth, 0, kScreenWidth, self.view.frame.size.height - kSegmentH -60);
        [sv addSubview:vc.view];
        
    }
    
    sv.contentSize = CGSizeMake(self.segment.titleList.count * kScreenWidth, 0);
    self.contentScrollView = sv;
}
//加载3个ViewController
-(void)addChildViewController{
    
    
    self.video = [ZYGoldMedalsController new];
    self.live = [ZYStrengthenClassController new];
    self.nearby = [ZYPeopleOneController new];
    ProgramStudyViewController * ProgramVC = [ProgramStudyViewController new];
    
//    self.video.ID = self.ID;
//    self.live.ID = self.ID;
//    self.nearby.ID = self.ID;
    
    [self addChildViewController:self.video];
    [self addChildViewController:self.live];
    [self addChildViewController:self.nearby];
    [self addChildViewController:ProgramVC];
    
    
    
}

#pragma mark - UIScrollViewDelegate
//实现LGSegment代理方法
-(void)scrollToPage:(int)Page {
    
    
    NSLog(@"%d",Page);
    
    self.page = Page;
    
    CGPoint offset = self.contentScrollView.contentOffset;
    offset.x = self.view.frame.size.width * Page;
    [UIView animateWithDuration:0.3 animations:^{
        self.contentScrollView.contentOffset = offset;
        
        
        
    }];
}
// 只要滚动UIScrollView就会调用
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    [self.searchTextField resignFirstResponder];
    
    CGFloat offsetX = scrollView.contentOffset.x;
    
    
    
    [self.segment moveToOffsetX:offsetX];
    
}


- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    CGFloat offsetX = scrollView.contentOffset.x;
    int page = offsetX/kScreenWidth;
    
    
    self.page = page;
    
}









- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    
    [self.searchTextField resignFirstResponder];
    
    
    if (0 == self.page) {
   
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"zyGold" object:textField.text];
         
        
        
        
    }else if (1 == self.page){
        [[NSNotificationCenter defaultCenter] postNotificationName:@"zyStrong" object:textField.text];
        
    }else{
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"zyThrees" object:textField.text];
    }
    
    
    
    return YES;
}



- (void)viewWillDisappear:(BOOL)animated{
    
    [super viewWillDisappear:animated];
    [SVProgressHUD dismiss];
    [self.searchTextField resignFirstResponder];
    
    
}





@end
